package com.yingpai.fitness.dvp_2_0.main.activity.shop.assmble;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.CartActivity;
import com.yingpai.fitness.dvp_2_0.main.adapter.shop.CustomPagerAdapter;
import com.yingpai.fitness.dvp_2_0.main.bean.assmble.AssmbleShopListBean;
import com.yingpai.fitness.dvp_2_0.main.fragment.shop.AssmbleShopSaleFragment;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class AssmbleShopSaleActivity extends CommonActivity implements OnTabSelectListener {


    @BindView(R.id.shop_sale_sliding_tab)
    SlidingTabLayout shopSaleSlidingTab;
    @BindView(R.id.shop_sale_vp)
    CustomViewPager shopSaleVp;
    private CustomPagerAdapter mAdapter;
    private ArrayList<Fragment> mList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.shop_sale_activity);
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HashMap<String, Object> httpParams = new HashMap();
//        httpParams.put("categoryId", 1);
        httpParams.put("pageNum", 1);
        httpParams.put("pageSize", 1);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_SHOP_LIST, httpParams, Url.ASSMBLE_SHOP_LIST, new Callback<AssmbleShopListBean>() {
            @Override
            public void onSuccess(AssmbleShopListBean assmbleShopListBean) {
                if (Constant.REQUEST_SUCCESS_0.equals(assmbleShopListBean.getCode())) {
                    shopSaleVp.setOffscreenPageLimit(assmbleShopListBean.getData().getCategoryList().size());

                    String[] mTitlesTopTab = new String[assmbleShopListBean.getData().getCategoryList().size()];

                    for (int i = 0; i < assmbleShopListBean.getData().getCategoryList().size(); i++) {
                        mTitlesTopTab[i] = assmbleShopListBean.getData().getCategoryList().get(i).getCategoryName();
                        mList.add(AssmbleShopSaleFragment.getInstance(assmbleShopListBean.getData().getCategoryList().get(i).getId()));
                    }

                    mAdapter = new CustomPagerAdapter(getSupportFragmentManager(), mList, mTitlesTopTab);

                    shopSaleVp.setAdapter(mAdapter);//配置适配器

                    shopSaleSlidingTab.setViewPager(shopSaleVp);//关联适配器
                } else {
                    showToast(assmbleShopListBean.getMessage()+"");
                }
                dismissDialog();
            }
        });
    }

    @Override
    protected void initView() {
        shopSaleSlidingTab.setOnTabSelectListener(this);

        setTitle("必拼商品");
    }

    /**
     * tab 选中
     *
     * @param position
     */
    @Override
    public void onTabSelect(int position) {
        shopSaleSlidingTab.setCurrentTab(position);
        shopSaleSlidingTab.setCurrentTab(position, false);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    @OnClick({R.id.shop_cart_iv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.shop_cart_iv:
                startActivity(CartActivity.class);
                break;
        }
    }
}
